﻿ <?php
ignore_user_abort(TRUE);
require_once("./interface.php");
require_once("./utils.php");
require_once("./interface.php");
require_once("./TechnicFunc.php");
require_once("./SoldierFunc.php");
require_once("./HotelFunc.php");
require_once("./OfficeFunc.php");
require_once("./MarketFunc.php");
require_once("./UnionFunc.php");
require_once("./StoreFunc.php");
require_once("./TroopFunc.php");
require_once("./global.php");
require_once("./report.php");



function HandleRanks()//定义排行榜队列
{
/*
	$con = mysql_connect("127.0.0.1","root","password");
mysql_query("DROP TABLE IF EXISTS sys_user_cityresource ");//创立 sys_user_cityresource 表
$sql = "CREATE TABLE sys_user_cityresource 
(
cid int(11) ,
uid int(11) ,
people int(11),
PRIMARY KEY (cid)
)";
mysql_query($sql,$con);
*/
//触发前线清空相关表数据
$r_t=time();
mysql_query("Update mem_state set value='$r_t' where state='10' ");
mysql_query("DELETE from sys_user_cityresource ");
mysql_query("DELETE from rank_user ");
mysql_query("DELETE from rank_hero ");
mysql_query("DELETE from rank_hero_affairs ");
mysql_query("DELETE from rank_hero_bravery ");
mysql_query("DELETE from rank_hero_wisdom ");
mysql_query("DELETE from rank_union ");
mysql_query("DELETE from rank_city");
mysql_query("DELETE from rank_city_type");

//===============统计城池人口总数
$citys=sql_fetch_into_arrays("select * from sys_city order by uid desc");//用户城池表
$j=0;
while($city_uid=$citys['uid'][$j]!="")
  {

$city_uid=$citys['uid'][$j];
$city_name=$citys['name'][$j];
$city_type=$citys['type'][$j];
$city_cid=$citys['cid'][$j];
$city_users=sql_fetch_into_arrays("select * from sys_user where uid='$city_uid'");
$city_user_name=$city_users['name'][0];
$city_union_id=$city_users['union_id'][0];
$cityunion_name=sql_fetch_into_arrays("select * from sys_union where id='$city_union_id' ");
$city_union_name=$cityunion_name['name'][0];
$citys_r=sql_fetch_into_arrays("select * from mem_city_resource where cid='$city_cid'");//mem_city_resource 取数
$citys_r_people=$citys_r['people'][0];
mysql_query("INSERT INTO sys_user_cityresource (name,cid,uid,people,type,`union`) 
VALUES ('$city_name','$city_cid', '$city_uid', '$citys_r_people','$city_type','$city_union_name')");

  $j=$j+1;
  }
//==================================================玩家排行
$users=sql_fetch_into_arrays("select * from sys_user where passtype!='npc' order by nobility desc,prestige desc ");//用户表
$juewei=sql_fetch_into_arrays("select * from cfg_nobility order by id ");//爵位表
$unions=sql_fetch_into_arrays("select * from sys_union order by id ");//union_id联盟ID
$citys=sql_fetch_into_arrays("select * from sys_city order by uid desc");//用户城池表
$i=0;
$k=1;
while($users['name'][$i]!="")
  {
$jwid=$users['nobility'][$i] ;//要显示用户的爵位ID
$jw=$juewei['name'][$jwid];//对应的爵位名称
$union_id=$users['union_id'][$i];//要显示用户的联盟ID
$union_name=sql_fetch_into_arrays("select * from sys_union where id='$union_id' ");//union_id联盟ID
$uid=$users['uid'][$i];
$city_count=sql_fetch_into_arrays("select count(*) from sys_city where uid='$uid' ");//玩家城池数目
$city_r_u=sql_fetch_into_arrays("SELECT SUM(people) AS peoples FROM sys_user_cityresource where uid='$uid' order by peoples desc ");
$r_u_name=$users['name'][$i];
$r_u_lm=$union_name['name'][0];
$r_u_sw=$users['prestige'][$i];
$r_u_citys=$city_count['count(*)'][0];
$r_u_peoples=$city_r_u['peoples'][0];

mysql_query("INSERT INTO rank_user (rank,uid,name,city,people,prestige,nobility,`union`) 
VALUES ('$k','$uid','$r_u_name','$r_u_citys','$r_u_peoples','$r_u_sw','$jw','$r_u_lm')");

  $i=$i+1;
  $k++;
  }

$d=0;
$ranks=sql_fetch_into_arrays("select * from rank_user order by rank ");//君主名称
while($ranks['rank'][$d]!="")
  {

$rank_s_u=$ranks['rank'][$d];
$uid_s_u=$ranks['uid'][$d];
mysql_query("UPDATE sys_user SET rank = $rank_s_u WHERE uid='$uid_s_u'");

$d=$d+1;
  }
 //==============================英雄排行 按等级排行
$heros=sql_fetch_into_arrays("select * from sys_city_hero where command_base!=0 order by level desc,command_base desc ");//将领表

$a=0;
$b=1;
while($heros['name'][$a]!="")
  {
$heros_uid=$heros['uid'][$a];
$junzhu=sql_fetch_into_arrays("select * from sys_user where uid=$heros_uid ");//君主名称
$sum_command=$heros['command_base'][$a]+$heros['command_add_on'][$a]+$heros['level'][$a];//统帅
$sum_affairs=$heros['affairs_base'][$a]+$heros['affairs_add_on'][$a]+$heros['affairs_add'][$a];//内政
$sum_bravery=$heros['bravery_base'][$a]+$heros['bravery_add_on'][$a]+$heros['bravery_add'][$a];//勇武
$sum_wisdom=$heros['wisdom_base'][$a]+$heros['wisdom_add_on'][$a]+$heros['wisdom_add_on'][$a];//智谋
$hero_hid=$heros['hid'][$a];
$hero_name=$heros['name'][$a];
$hero_junzhu=$junzhu['name'][0];
$hero_level=$heros['level'][$a];
mysql_query("INSERT INTO rank_hero (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$b', '$hero_hid', '$hero_name','$hero_junzhu','$hero_level','$sum_affairs','$sum_bravery','$sum_wisdom')");


	
  $a=$a+1;
  $b=$b+1;
  }


//=============将领按内政、勇武、智谋排行
$heros_a=sql_fetch_into_arrays("select * from rank_hero order by affairs desc,level desc");//将领排行表 按内政排行
$heros_b=sql_fetch_into_arrays("select * from rank_hero order by bravery desc,level desc ");//将领排行表 按勇武排行
$heros_w=sql_fetch_into_arrays("select * from rank_hero order by wisdom desc,level desc ");//将领排行表 按智谋排行
$i=0;
$k=1;
while($heros_a['name'][$i]!="")
  {
//内政排行变量定义
$h_hid_a=$heros_a['hid'][$i];
$h_name_a=$heros_a['name'][$i];
$h_user_a=$heros_a['user'][$i];
$h_level_a=$heros_a['level'][$i];
$h_affairs_a=$heros_a['affairs'][$i];
$h_ubravery_a=$heros_a['bravery'][$i];
$h_wisdom_a=$heros_a['wisdom'][$i];
//勇武排行变量定义
$h_hid_b=$heros_b['hid'][$i];
$h_name_b=$heros_b['name'][$i];
$h_user_b=$heros_b['user'][$i];
$h_level_b=$heros_b['level'][$i];
$h_affairs_b=$heros_b['affairs'][$i];
$h_ubravery_b=$heros_b['bravery'][$i];
$h_wisdom_b=$heros_b['wisdom'][$i];
//智谋排行变量定义
$h_hid_w=$heros_w['hid'][$i];
$h_name_w=$heros_w['name'][$i];
$h_user_w=$heros_w['user'][$i];
$h_level_w=$heros_w['level'][$i];
$h_affairs_w=$heros_w['affairs'][$i];
$h_ubravery_w=$heros_w['bravery'][$i];
$h_wisdom_w=$heros_w['wisdom'][$i];
//写入rank_hero_affairs表
mysql_query("INSERT INTO rank_hero_affairs (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_a', '$h_name_a','$h_user_a','$h_level_a','$h_affairs_a','$h_ubravery_a','$h_wisdom_a')");
//写入rank_hero_bravery表
mysql_query("INSERT INTO rank_hero_bravery (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_b', '$h_name_b','$h_user_b','$h_level_b','$h_affairs_b','$h_ubravery_b','$h_wisdom_b')");
//写入rank_hero_wisdom表
mysql_query("INSERT INTO rank_hero_wisdom (rank,hid,name,user,level,affairs,bravery,wisdom) 
VALUES ('$k', '$h_hid_w', '$h_name_w','$h_user_w','$h_level_w','$h_affairs_w','$h_ubravery_w','$h_wisdom_w')");
  $i=$i+1;
  $k=$k+1;
  }
 //==========================以下为联盟排行

$lm_unions=sql_fetch_into_arrays("select * from sys_union  order by prestige desc ");//用户表
$a=0;
$b=1;
while($lm_unions['name'][$a]!="")
  {
$lm_leader_uid=$lm_unions['leader'][$a];
$lm_un_id=$lm_unions['id'][$a];
$lm_users=sql_fetch_into_arrays("select * from sys_user where uid='$lm_leader_uid' ");//用户表
$lm_users_sw=sql_fetch_into_arrays("select SUM(prestige) as prestiges from sys_user where union_id='$lm_un_id' ");//用户表
$lm_un_name=$lm_unions['name'][$a];
$lm_u_name=$lm_users['name'][0];
$lm_un_member=$lm_unions['member'][$a];
$lm_un_prestige=$lm_users_sw['prestiges'][0];

mysql_query("UPDATE sys_union SET prestige =$lm_un_prestige WHERE id='$lm_un_id'");
mysql_query("INSERT INTO rank_union (rank,uid,name,leader,member,prestige) 
VALUES ('$b', '$lm_un_id', '$lm_un_name','$lm_u_name','$lm_un_member','$lm_un_prestige')");

//echo $b."--".$lm_un_id."--".$lm_un_name."--".$lm_u_name."--".$lm_un_member."--".$lm_un_prestige."<br>";
	
  $a=$a+1;
  $b=$b+1;
  }


//==================================以下为城池排行
$r_citys1=sql_fetch_into_arrays("select * from sys_user_cityresource order by people desc ");//用户表
$t=0;
$b=1;
while($r_citys1['name'][$t]!="")
        {
$city_name1=$r_citys1['name'][$t];  
$city_type1=$r_citys1['type'][$t];
$city_cid1=$r_citys1['cid'][$t];
$city_uid1=$r_citys1['uid'][$t];
$city_union=$r_citys1['union'][$t];
$city_people1=$r_citys1['people'][$t]; 
$uid_name1=sql_fetch_one_cell("select name from sys_user where uid='$city_uid1'");

//mysql_query("INSERT INTO rank_city (rank,name,type,user,'union',cid,people) 
//VALUES ('$b','$city_name1', '$city_type1','$uid_name1','$city_union','$city_cid1','$city_people1')"); 

mysql_query("INSERT INTO  rank_city (rank,name,type,user,`union`,cid,people) 
VALUES ('$b','$city_name1', '$city_type1','$uid_name1','$city_union','$city_cid1','$city_people1')"); 

  $t=$t+1;
  $b=$b+1;
    }


//======================城池类型排行

$city_type=sql_fetch_into_arrays("select * from sys_user_cityresource order by type desc ");
$z=0;
$k=1;
while($city_type['name'][$z]!="")
    {
$citytype_type=$city_type['type'][$z];
$citytype_name=$city_type['name'][$z];
$citytype_cid=$city_type['cid'][$z];
$citytype_uid=$$city_type['uid'][$z];
$cityuser=sql_fetch_one_cell("select name from sys_user where uid='$citytype_uid'");
$citytype_people=$city_type['people'][$z];
$citytype_union=$city_type['union'][$z];
mysql_query("INSERT INTO rank_city_type (rank,name,type,user,`union`,cid,people) 
VALUES ('$k', '$citytype_name', $citytype_type, '$cityuser','$citytype_union','$citytype_cid','$citytype_people')");

  $z=$z+1;
  $k=$k+1;
    }


}


?>



